/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.controller.rms;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Stack;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Victorino
 */
public class RecordStorePool {

    private Hashtable pool = new Hashtable();
    private static RecordStorePool instance = new RecordStorePool();

    public static RecordStorePool getInstance() {
        return instance;
    }

    public RecordStore getRecordStore(int mObjectLoader) {

        Integer it = new Integer(mObjectLoader);
        RecordStore rms = (RecordStore) pool.get(it);
        if (rms == null) {
            try {
                rms = RecordStore.openRecordStore(it.toString() + "_yes", true);

                pool.put(new Integer(mObjectLoader), rms);
            } catch (Exception e) {
                System.out.println("Erro " + it);
                e.printStackTrace();
            }
        }

        return rms;
    }

    public void removeRecordStores(Vector mObjectLoaders) {

        for (int i = 0; i < mObjectLoaders.size(); i++) {
            Integer c = (Integer) mObjectLoaders.elementAt(i);
            RecordStore rms = this.getRecordStore(c.intValue());
            if (rms != null) {
                try {
                    rms.closeRecordStore();
                    RecordStore.deleteRecordStore(c.toString());
                    pool.remove(c);
                } catch (RecordStoreNotOpenException ex) {
                    ex.printStackTrace();
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
            }
            getRecordStore(c.intValue()); // reopens all recordSotre
        }
    }

    public void closeAllRecordStore() {

        Enumeration enu = pool.keys();
        while (enu.hasMoreElements()) {
            Integer key = (Integer) enu.nextElement();
            RecordStore rms = (RecordStore) pool.get(key);
            if (rms == null) {
                try {
                    rms.closeRecordStore();
                    pool.remove(key);
                } catch (RecordStoreNotOpenException ex) {
                    ex.printStackTrace();
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    public int getNumRecords(int mObjectLoader) {
        int max = -1;
        try {
            max = getRecordStore(mObjectLoader).getNumRecords();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }

        return max;
    }

    public void deleteRecord(int mObjectLoader, int id) {
        try {
            RecordStore rms = getRecordStore(mObjectLoader);
            rms.deleteRecord(id);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            System.out.println("Erro " + mObjectLoader + " Id: " + id);
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

    }

    public byte[] getRecord(int mObjectLoader, Integer id) {
        byte[] retVal = null;
        try {

            retVal = getRecordStore(mObjectLoader).getRecord(id.intValue());

        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            System.out.println("Erro: " + mObjectLoader + " id: " + id);
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        return retVal;
    }

    public int getNextRecordId(int mObjectLoader) {
        try {
            return getRecordStore(mObjectLoader).getNextRecordID();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        return 0;
    }

    public int storeRecord(int mObjectLoader, byte[] data) {
        int ret = 0;
        try {
            ret = this.getRecordStore(mObjectLoader).addRecord(data, 0, data.length);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreFullException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        data = null;
        return ret;
    }

    public void udpateRecord(int mObjectLoader, int id, byte[] data) {
        try {

            this.getRecordStore(mObjectLoader).setRecord(id, data, 0, data.length);

        } catch (Exception ex) {
            System.out.println("Erro " + mObjectLoader + " Id: " + id);
            ex.printStackTrace();
        }
    }
    
    public int getSizeAvaliable(int mObjectLoader){
        try {
            return this.getRecordStore(mObjectLoader).getSizeAvailable();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public Stack getAllRecords(int mObjectLoader) {

        Stack lista = null;
        try {
            RecordStore rms = getRecordStore(mObjectLoader);
            if (rms != null) {

                RecordEnumeration num = rms.enumerateRecords(null, null, false);
                lista = new Stack();
                while (num.hasPreviousElement()) {
                    lista.addElement(num.previousRecord());
                }

            }
        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        return lista;
    }

    public void removeAll(int mObjectLoader) {
        try {
            RecordEnumeration num;
            num = getRecordStore(mObjectLoader).enumerateRecords(null, null, false);
            while (num.hasPreviousElement()) {
                int i = num.previousRecordId();
                this.deleteRecord(mObjectLoader, i);
            }

        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
}
